package com.scheduler;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.constant.SharesConstan;
import com.common.utils.DateUtil;
import com.entity.*;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Component
@Configurable
@EnableScheduling
public class SharesSch {

    @Autowired
    private SharesGdnumService sharesGdnumService;

    @Autowired
    private CodeCjlService codeCjlService;

    @Autowired
    private ShareHolderService shareHolderService;

    @Autowired
    private SharesReviewService sharesReviewService;

    @Autowired
    private SharesDetailService sharesDetailService;

    @Autowired
    private SharesDataService sharesDataService;

    @Autowired
    private HintNumService hintNumService;


    /**
     * 5转以上的提示
     */

    /**
     * 8
     * 每天收盘结束后更新必要数据
     */
    @Scheduled(cron = "* 05 15 * * ?")
    public void updateHint(){
        if(SharesConstan.FLAG){
            return;
        }
        //保存复盘数据
        sharesReviewService.saveData();

    }

    /**
     * 板块资金和概念资金更新
     * 5分钟更新一次
     */
    @Scheduled(cron = "* 0/5 * * * ?")
    public void getzjlx(){
        if(SharesConstan.FLAG){
            return;
        }
        boolean moTime = DateUtil.judgeIsSetBiddingTime("9:28", SharesConstan.START_TIME);
        boolean afTime = DateUtil.judgeIsSetBiddingTime("12:59", SharesConstan.END_TIME);
        if(!moTime&&!afTime){//不在开盘时间段
            return;
        }
        shareHolderService.getzjlx();
        //存储提示数据
        hintNumService.saveNum();
    }

    @Scheduled(cron = "* 0/5 * * * ?")
    public void gethyssl(){
        if(SharesConstan.FLAG){
            return;
        }
        boolean moTime = DateUtil.judgeIsSetBiddingTime("9:28", SharesConstan.START_TIME);
        boolean afTime = DateUtil.judgeIsSetBiddingTime("12:59", SharesConstan.END_TIME);
        if(!moTime&&!afTime){//不在开盘时间段
            return;
        }
        shareHolderService.gethyssl();
    }

    @Scheduled(cron = "0/10 * * * * ?")
    public void getSharesCode(){
        if(SharesConstan.FLAG){
            return;
        }

//        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
//        String httpArg = f.format(new Date());
//        String jsonResult = HolidayUtil.request(httpArg);
//        if (!"0".equals(jsonResult)) {//3 6 12 24 48 96 128 256 512 1024 2048 4096 8182 16364
//            return  ;//            System.out.println("0上班");
//        }

        boolean moTime = DateUtil.judgeIsSetBiddingTime("9:28", SharesConstan.START_TIME);
        boolean afTime = DateUtil.judgeIsSetBiddingTime("12:55", SharesConstan.END_TIME);
        if(!moTime&&!afTime){//不在开盘时间段
            return;
        }
//        List<CodeCjl> lists=codeCjlService.getCodeCjls(SharesConstan.CODE_RED_LIST);
        List<CodeCjl> lists=codeCjlService.getCodeCjlsByTx(SharesConstan.CODE_RED_LIST);
        codeCjlService.hint(lists,SharesConstan.RED);
    }

    /**
     * 每个月，每周，振幅最大的股
     */

    /**
     * 周涨幅排名。
     */

    /**
     * 涨幅达到百分之10标记一下
     * 每天多少时间提示的频率，与大盘的关系
     */
//    public void

    /**
     * 每天前25出现的，回测较低的，无脑买入
     */

    /**
     * 突破买入，回到均线买入，买点。
     */



    @Scheduled(cron = "* 10 9 * * ?")
    public void init() {

        if(SharesConstan.FLAG){
            return;
        }
        shareHolderService.init();
    }

    /**
     * 代写：提示的间隔 9.40-9.50
     */

    /**
     * 每天8点更新
     */
    @Scheduled(cron = "* 30 8 * * ?")
    public void updateShares() {
        if(SharesConstan.FLAG){
            return;
        }
        shareHolderService.updateShares();
        System.out.println("sout");
        //更新前一天的数据
        shareHolderService.getshares();
        //跟涨数据
        sharesDataService.getDataByYear();
        //更新创业板指
        sharesDataService.getsz();
        //历史提示数据
        sharesDataService.getTsData();
    }

    /**
     * 股东变化
     * 增减持
     * 个股公告
     */
    @Scheduled(cron = "* 10 8 * * ?")
    public void updateOthre() {
        if(SharesConstan.FLAG){
            return;
        }
        shareHolderService.updateOthre();
    }

    /**
     * 最近公布，且筹码减少百分之10的
     */
    public void subNum(){
        sharesGdnumService.subNum();
    }

    /**
     * 周日初始化数据
     */
    @Scheduled(cron = "* 10 13 * * ?")
    public void getInitData(){
         //判断今天是否周日
         Calendar cal = Calendar.getInstance();
         cal.setTime(new Date());
        //   if (cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
        if (cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
            Wrapper<SharesDetail> wrd=new EntityWrapper<>();
            List<SharesDetail> sharesDetails = sharesDetailService.selectList(wrd);
            Wrapper<SharesDetail> wr=new EntityWrapper<>();
            sharesDetailService.delete(wr);

            List<SharesDetail> saveList=new ArrayList<>();
            for (SharesDetail sharesDetail : sharesDetails) {
                SharesDetail vo=new SharesDetail()  ;
                vo.setCode(sharesDetail.getCode());
                vo.setName(sharesDetail.getName());
                vo.setSshy(sharesDetail.getSshy());
                vo.setSsgn(sharesDetail.getSsgn());
                vo.setMagical(sharesDetail.getMagical());
                vo.setStatus(88);
                saveList.add(vo);
            }
            sharesDetailService.insertBatch(saveList);

            Wrapper<SharesData> wrdd=new EntityWrapper<>();
            sharesDataService.delete(wrdd);
            //获取周数据 //获取上周收盘价
            sharesDataService.getWeekData();
            //获取月数据//获取上月收盘价
            sharesDataService.getMonthData();
            //计算周涨幅 //记录周涨幅
            sharesDataService.getzzf();
            //计算月涨幅  //记录月涨幅
            sharesDataService.getyzf();
        }

        //判断下周是否1号

    }

    /**
     * 行业公告，分词，占比，是否存在股票相关行业公告
     * 不能带
     */

    /**
     * 前25提示花费时间，
     * 前30提示花费时间
     */




}
